package com.ruoyi.web.TemuProductTraffic.service.impl;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;

import cn.hutool.core.date.DateUtil;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.ruoyi.web.TemuEvents.domain.TemuEvents;
import com.ruoyi.web.TemuFzProductRestriction.domain.TemuFzProductRestriction;
import com.ruoyi.web.TemuFzProductRestriction.mapper.TemuFzProductRestrictionMapper;
import com.ruoyi.web.utils.Httpapi;
import com.ruoyi.web.utils.TemuApi;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.web.TemuProductTraffic.mapper.TemuProductTrafficMapper;
import com.ruoyi.web.TemuProductTraffic.domain.TemuProductTraffic;
import com.ruoyi.web.TemuProductTraffic.service.ITemuProductTrafficService;

/**
 * TemuProductTrafficService业务层处理
 * 
 * @author ruoyi
 * @date 2025-08-07
 */
@Service
public class TemuProductTrafficServiceImpl implements ITemuProductTrafficService 
{
    @Autowired
    private TemuProductTrafficMapper temuProductTrafficMapper;

    @Autowired
    public TemuFzProductRestrictionMapper temuFzProductRestrictionMapper;

    /**
     * 查询TemuProductTraffic
     * 
     * @param id TemuProductTraffic主键
     * @return TemuProductTraffic
     */
    @Override
    public TemuProductTraffic selectTemuProductTrafficById(Integer id)
    {
        return temuProductTrafficMapper.selectTemuProductTrafficById(id);
    }

    /**
     * 查询TemuProductTraffic列表
     * 
     * @param temuProductTraffic TemuProductTraffic
     * @return TemuProductTraffic
     */
    @Override
    public List<TemuProductTraffic> selectTemuProductTrafficList(TemuProductTraffic temuProductTraffic)
    {
        return temuProductTrafficMapper.selectTemuProductTrafficList(temuProductTraffic);
    }

    /**
     * 新增TemuProductTraffic
     * 
     * @param temuProductTraffic TemuProductTraffic
     * @return 结果
     */
    @Override
    public int insertTemuProductTraffic(TemuProductTraffic temuProductTraffic)
    {
        return temuProductTrafficMapper.insertTemuProductTraffic(temuProductTraffic);
    }

    /**
     * 修改TemuProductTraffic
     * 
     * @param temuProductTraffic TemuProductTraffic
     * @return 结果
     */
    @Override
    public int updateTemuProductTraffic(TemuProductTraffic temuProductTraffic)
    {
        return temuProductTrafficMapper.updateTemuProductTraffic(temuProductTraffic);
    }

    /**
     * 批量删除TemuProductTraffic
     * 
     * @param ids 需要删除的TemuProductTraffic主键
     * @return 结果
     */
    @Override
    public int deleteTemuProductTrafficByIds(Integer[] ids)
    {
        return temuProductTrafficMapper.deleteTemuProductTrafficByIds(ids);
    }

    /**
     * 删除TemuProductTraffic信息
     * 
     * @param id TemuProductTraffic主键
     * @return 结果
     */
    @Override
    public int deleteTemuProductTrafficById(Integer id)
    {
        return temuProductTrafficMapper.deleteTemuProductTrafficById(id);
    }

    @Override
    public int insdatas(List<TemuProductTraffic> list) {
        return temuProductTrafficMapper.insdatas(list);
    }

    @Override
    public int apiinsdatas(String cookie, String mallId) {
        getproductdata(cookie, mallId);
        return 1;
    }

    private void getproductdata(String cookie, String mallid) {
        //活动
        int pages=999;
        int page=1;
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        LocalDate endDate = LocalDate.now();
        LocalDate startDate = endDate.minusDays(29); // 包含今天共30天

        String start = startDate.format(formatter);
        String end = endDate.format(formatter);
        while (page<=pages)
        {
            String bow=TemuApi.product_Wh_Head +"\""+start+"\""+TemuApi.product_Wh_Page+"\""+end+"\""+TemuApi.product_Wh_Warehouse+page+++TemuApi.product_Wh_end;
            String resp = Httpapi.post(TemuApi. productHistoryData, cookie, bow, mallid);
            JSONObject root = JSONUtil.parseObj(resp);
            int total = JSONUtil.parseObj(root.getStr("result")).getInt("total");

            if (total==0)
                break;
            if (pages==999)
                pages = (total+40-1)/40;

            //解析
            JSONObject result = root.getJSONObject("result");
            JSONArray salesDataVOList = result.getJSONArray("salesDataVOList");

            // 销量数据表
            List<TemuProductTraffic> list = new ArrayList<>();
            //辅助表
            List<TemuFzProductRestriction> fzlist = new ArrayList<>();
            for (int i = 0; i < salesDataVOList.size(); i++) {
                JSONObject item = salesDataVOList.getJSONObject(i);
                JSONObject basic = item.getJSONObject("productSkcBasicInfoVO");
                //fz填充
                TemuFzProductRestriction fzinfo = new TemuFzProductRestriction();
                fzinfo.img = basic.getStr("productSkcPicture");
                fzinfo.spuId = basic.getStr("productId");
                fzinfo.highPriceProductSearchLimit = item.getLong("highPriceProductSearchLimit");
                fzinfo.highPriceProductSearchLimitBeginTime = item.getDate("highPriceProductSearchLimitBeginTime");
                fzinfo.highPriceProductSearchLimitEndTime = item.getDate("highPriceProductSearchLimitEndTime");
                fzinfo.marketProductSearchLimitBeginTime = item.getDate("marketProductSearchLimitBeginTime");
                fzinfo.marketProductSearchLimitEndTime = item.getDate("marketProductSearchLimitEndTime");
                fzinfo.shortTermFlowGrowBeginTime = item.getDate("shortTermFlowGrowBeginTime");
                fzinfo.shortTermFlowGrowEndTime = item.getDate("shortTermFlowGrowEndTime");
                fzinfo.longTermFlowGrowBeginTime = item.getDate("longTermFlowGrowBeginTime");
                fzinfo.longTermFlowGrowEndTime = item.getDate("longTermFlowGrowEndTime");
                fzinfo.marketProductSearchLimitTag = item.getStr("marketProductSearchLimitTag");
                fzinfo.date = DateUtil.date();
                fzlist.add(fzinfo);
                /////
                JSONArray datalist = item.getJSONArray("confirmTrendList");
                for (Object dataitem : datalist) {
                    JSONObject datejson = JSONUtil.parseObj(dataitem);
                    TemuProductTraffic info = new TemuProductTraffic();
                    info.productName = basic.getStr("productName");
                    info.imgUrl = basic.getStr("productSkcPicture");
                    info.spuId = basic.getStr("productId");
                    info.day=datejson.getDate("day");
                    info.quantity=datejson.getInt("quantity");
                    info.confirmGoodsQuantity=item.getInt("confirmGoodsQuantity");
                    info.date= DateUtil.date();
                    list.add(info);
                }
            }
            temuFzProductRestrictionMapper.deledata(DateUtil.today());
            temuProductTrafficMapper.dellall();
            temuFzProductRestrictionMapper.insdatas(fzlist);
            temuProductTrafficMapper.insdatas(list);



        }
    }
}
